package com.cxl.demo.service;

import com.cxl.demo.mapper.AssigmentMapper;
import com.cxl.demo.mapper.RecordMapper;
import com.cxl.demo.mapper.UserMapper;
import com.cxl.demo.util.StaticValue;
import com.cxl.demo.util.TimeUpd;
import com.cxl.demo.vo.Assigment;
import com.cxl.demo.vo.Pager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.xml.crypto.Data;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service

public class AssigmentServiceImpl implements AssigmentService {


    @Autowired
    private AssigmentMapper assigmentMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RecordMapper recordMapper;

    @Override
    public Pager getMyAssigment(Map<String,Object> map) {
        Pager pager=new Pager();
        List<Assigment> list=assigmentMapper.findAssigmentByUserId(map);
        pager.setRows(list);
        pager.setTotal(assigmentMapper.getUserAssigmentNum((Integer) map.get("userId")));
        return pager;
    }

    @Override
    public Pager getAssigment(Map<String,Object> map) {
        Pager pager=new Pager();
        List<Assigment> list=assigmentMapper.findAssigmentList(map);
        pager.setRows(list);
        pager.setTotal(assigmentMapper.getAssigmentNum(map));
        return pager;
    }

    @Override
    public Integer updateAssigment(Assigment assigment) {
        Integer number=recordMapper.recordNumberByAssigmentId(assigment.getAssigmentId());
        if(number!=0){
            return 0;
        }
        return assigmentMapper.updateAssigment(assigment);
    }

    @Override
    public Integer addAssigment(Assigment assigment) {


                assigmentMapper.addAssigment(assigment);
        return 1;
    }

    @Override
    public Integer deleteAssigment(Integer assigmentId) {
        Assigment assigment=assigmentMapper.findAssigmentById(assigmentId);
        Integer number=recordMapper.recordNumberByAssigmentId(assigmentId);
        if(number!=0){
            return 0;
        }
        assigmentMapper.deleteAssigment(assigmentId);
        //返还积分
        userMapper.updateAddScore(assigment.getUserId(),assigment.getScore()*assigment.getNeedPeopleNumber());
        return 1;
    }

    @Override
    public Assigment getAssigmentById(Integer assigmentId) {
        Assigment assigment=assigmentMapper.findAssigmentById(assigmentId);

        return assigment;
    }

    @Override
    public Pager getAllEmergency(Map<String,Object> map) {

        Pager pager=new Pager();
        pager.setRows(assigmentMapper.getEmergencyNo1(map));
        pager.setTotal(assigmentMapper.getEmergencyNo1Num());
        return pager;
    }

    @Override
    public Object getUserVal(Integer userId) {

        Map<String,Object> map=new HashMap<>();
        map.put("page",0);
        map.put("userId",userId);
        map.put("size",assigmentMapper.getUserAssigmentNum(userId));
        List<Assigment> list=assigmentMapper.findAssigmentByUserId(map);
                for(Assigment assigment:list){

                }
        return null;
    }

    @Override
    public Object getRootVal() {



        return null;
    }

    @Override
    public Integer EditAssigment(Assigment assigment) {
        assigmentMapper.EditAssigment(assigment);
        return 1;
    }


    @Override
    public List<Assigment> getTuiJian() {



        String timestamp1= TimeUpd.getDate();
        Integer num= StaticValue.dataNum;
        List<Assigment> list=assigmentMapper.getEmergency();
        List<Assigment> list2=assigmentMapper.getEmergency();
        for (Assigment assigment:list){
            if(Math.abs(TimeUpd.getNum(timestamp1,assigment.getStartTime()))<num){
                list2.add(assigment);
            }
        }
        return list2;
    }


}
